package tinycloud.net;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;

import org.apache.http.entity.FileEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

/**
 * La classe client envoie les données sur le serveur.
 * @author Nicolas CHARREL
 */
public class Client {

    private String server;
    private String port;

    public Client(String server, String port) {
        this.server = server;
        this.port = port;
    }

    public boolean upload(String path, String filename) {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost method = new HttpPost(server + "/auth/login");
            ArrayList <NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("name", "admin"));
            nvps.add(new BasicNameValuePair("password", "admin"));
            method.setEntity(new UrlEncodedFormEntity(nvps));
            client.execute(method);

            File file = new File(filename);
            method = new HttpPost(server + "/file/upload");
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("path", new StringBody(path));
            entity.addPart("file", new FileBody(file));
            method.setEntity(entity);
            client.execute(method);
        } catch(Exception e) {
            System.out.println(e);
            return false;
        }
        return true;
    }
}
